#Author:Math2Gold ( http://groups.google.com/group/math2gold ) 
#Last update:2009 Sep 11

import urllib
import urllib2
import urlparse
    
def sendAuthHTTPContent(user,pwd,uri,postdata):
    url=uri
    user_agent = 'Math2Gold Messager'
    headers = { 'User-Agent' : user_agent }
    
    auth = urllib2.HTTPBasicAuthHandler()
    netlock = urlparse.urlparse(url)
    passwdMgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passwdMgr.add_password(None,netlock[1],user,pwd)
    
    auth = urllib2.HTTPBasicAuthHandler(passwdMgr)
    req = urllib2.Request(url,postdata,headers)
    op = urllib2.build_opener(auth)
    response = op.open(req)
    document = response.read()
    return document
    

def sendMBlog(msg,user,pwd,uri='https://twitter.com/statuses/update.json',prefix='status='):
    return sendAuthHTTPContent(user,pwd,uri,prefix+msg)
    
#####################################################################################

